<?php
class Admin_ArticleController extends Apps_Controller_Action_Admin {

	//LIST
	public function indexAction() {
		$this->actResolve('act_delete',
						  'delete', null, null, array('id' => $this->_getParam('item_checker')),
					 	  true);

		$tbArticle = new M_DbTable_Article();
		$grid 	= new Apps_Plugin_Grid('ArticleGrid');

		$grid->setData($tbArticle->getList()) 
			 ->setTableCaption('List Article')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'width'		=> '5%',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Title',
			 			  'dataField'	=> 'en_title'),
			 		array('title'		=> 'About',
			 			  'width'		=> '10%',
	                      'html'        => function($rs) {
	                                            $labelArticleReference = new Apps_View_Helper_LabelArticleReference();
	                                            return $labelArticleReference->labelEnquiresReferenceType($rs['reference_type']);
	                                        }),
			 		array('title'		=> 'Public',
			 			  'width'		=> '5%',
			 			  'html'	    => function ($rs) {
			 			  						return ($rs['status'] == M_DbTable_Tour::STATUS_ACTIVE)?
			 			  						'<a href="##" class="ajax-link-must-confirm popup hint  label label-success" data-ajax-to="/admin/article/change-value?p=public&id=' . $rs['id'] . '" data-must-confirm-msg="Do you want to change this ?" title="Thay đổi" data-after-ajax="No">Yes</a>' :
			 			  						'<a href="##" class="ajax-link-must-confirm popup hint  label label-warning" data-ajax-to="/admin/article/change-value?p=public&id=' . $rs['id'] . '" data-must-confirm-msg="Do you want to change this ?" title="Thay đổi" data-after-ajax="Yes">No</a>';
											},
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Created Date',
			 			  'width'		=> '7%',
			 			  'html'	    => function ($rs) {
			 			  						return substr($rs['created_date'], 0, 10);
											}),
			 		array('title'       => 'Edit',
			 			  'html'        => array('<a href="/admin/article/edit?id=%s"><i class="icon-edit" title="Sửa"></i></a>',
			 			  				   array('id')),
			 			  'width'       => '2%'),
			 		array('title'       => 'Delete',
			 			  'html'        => array('<a href="/admin/article/delete?id=%s"><i class="icon-remove must-confirm" data-must-confirm-msg="Bạn có chắc muốn xóa bài viết này ?" title="Xóa"></i></a>',
			 			  				   array('id')),
			 			  'width'       => '2%')
			 		));
		$this->view->grid = $grid;
	}

	//ADD
	public function addAction() {
		$rq 		= $this->_request;
		$f 			= new Admin_Form_AddArticleNomal();
		$tbArticle	= new M_DbTable_Article();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$data = $f->getValues();

				$this->_flashMessenger->addMessage(
					$tbArticle->safelyInsert($data)
					? self::$msgAddSuccess
					: self::$msgAddFailed
				);

				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
	}

	//EDIT Article
	public function editAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		$tbArticle = new M_DbTable_Article();

		if (!$id) {
			$this->_helper->redirector('index');
		}

		$f = new Admin_Form_AddArticleNomal();
		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$tbArticle 	= new M_DbTable_Article();

				// Upload thumbnail - replace thumbnail if changed
				$rs = $tbArticle->getArticleById($id)->query()->fetch();
				$data = $f->getValues();
				$rT = $tbArticle->safelyUpdate($data, array('id = ?' => $id));
				if (!$rT) {
					$this->_flashMessenger->addMessage(self::$msgUpdateFailed);
				} else {
					$this->_flashMessenger->addMessage(self::$msgUpdateSuccess);
				}
				$this->_helper->redirector('index');
			}
		}
		$f->setLegend('Edit Article');
		$f->populate($tbArticle->fetchRow(array('id = ?' => $id))->toArray());
		$this->view->form = $f;
		$this->render('add');
	}

	//DELETE
	public function deleteAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		if (!$id) {
			$this->_helper->redirector('index');
		}

		$tbArticle = new M_DbTable_Article();
		$tbArticle = new M_DbTable_Article();

		if (is_numeric($id)) {
			$this->_flashMessenger->addMessage($tbArticle->deleteArticleById($id)
				? self::$msgDeleteSuccess
				: self::$msgDeleteError);
		} elseif (is_array($id)) {
			foreach ($id as $v) {
				$tbArticle->deleteArticleById($v);
			}
			$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
		}

		$this->_helper->redirector('index');
	}

	public function changeValueAction() {
		$this->disableLayout(true, true);
		$rq = $this->_request;
		$param  = $rq->getParam('p');
		$id 	= intval($rq->getParam('id'));
		$tbArticle = new M_DbTable_Article();
		if(!($r = $tbArticle->fetchRow(array('id = ?' => $id))->toArray())) {
			echo '<script>window.location=window.location</script>
				<a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
				. $this->getBacktrackUrl()
				. '">Update Error (Record not found), please wait ...</a>';
		}

		switch ($param) {
			case 'public':
				($r['status'] == M_DbTable_Article::STATUS_ACTIVE) ?
					$tbArticle->safelyUpdate(array('status'  => M_DbTable_Article::STATUS_INACTIVE),
										  	 array('id = ?'	  => $id)) :
					$tbArticle->safelyUpdate(array('status'  => M_DbTable_Article::STATUS_ACTIVE),
										   	 array('id = ?'  => $id));
				break;
			default: break;
		}

		echo '<script>document.location.reload()</script>
			<a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
			. $this->getBacktrackUrl()
			. '">Update successfully, please wait ...</a>';
	}

}